import functools

import grpc
from blog.proto import blog_pb2, blog_pb2_grpc


def run(func):
    def wrapper(*args, **kwargs):
        with grpc.insecure_channel('localhost:10059') as channel:
            stub = blog_pb2_grpc.BlogServiceStub(channel)
            return func(stub)
    return wrapper


@run
def article_list(stub):
    response = stub.ArticleList(blog_pb2.Page(page=1, pageSize=5))
    print(response.total)
    for a in response.data:
        print(a)
    return response


@run
def create(stub):
    resp = stub.CreateArticle(blog_pb2.CreateArticleRequest(
        title='hello2', author='liang', summary='hello2', content='hello2, hello2'
    ))
    print(resp)


@run
def update(stub):
    resp = stub.ArticleUpdate(blog_pb2.ArticleInfo(
        id=1, title='hello11', author='liang11', summary='11', content='11, hello2'
    ))
    print(resp)


if __name__ == '__main__':
    # article_list()
    create()
    # update()
